/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.validator;

import com.minierp.bean.MakeTypeBean;
import com.minierp.dao.MakeTypeDao;
import com.minierp.model.MakeType;
import java.math.BigDecimal;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class MakeTypeValidator implements Validator {
	private MakeTypeDao makeTypeDao;

	public boolean supports(Class clazz) {
		return MakeTypeBean.class.isAssignableFrom(clazz);
	}

	public void validate(Object obj, Errors errors) {
		MakeTypeBean bean = (MakeTypeBean) obj;
		MakeType form = bean.getMakeType();
		boolean hasError = false;
		if (form.getTypeName() != null && !form.getTypeName().trim().equals("")) {
			if (form.getParentId() != 0L && form.getId() != 0L) {
				if (form.getParentId() == form.getId()) {
					errors.rejectValue("makeType.parentId", "", "不能选择自己作为『上级』");
					hasError = true;
				} else {
					MakeType e = (MakeType) this.makeTypeDao.get(
							MakeType.class, form.getParentId());
					if (e != null
							&& e.getParentIdStr() != null
							&& e.getParentIdStr().indexOf(
									";" + form.getId() + ";") != -1) {
						errors.rejectValue("makeType.parentId", "",
								"不能选择自己的下级作为『上级』");
						hasError = true;
					}
				}
			}
		} else {
			errors.rejectValue("makeType.typeName", "", "请输入『名称』");
			hasError = true;
		}

		if (!hasError && (form.getType() != 1 || form.getWorkCenterId() != 0L)
				&& form.getType() == 2) {
			form.setWorkCenterId(0L);
		}

		if (!hasError) {
			if (bean.getLossRateStr() != null
					&& !bean.getLossRateStr().equals("")) {
				try {
					form.setLossRate(new BigDecimal(bean.getLossRateStr()));
					if (form.getLossRate().compareTo(new BigDecimal(100)) >= 0
							|| form.getLossRate().compareTo(new BigDecimal(0)) < 0) {
						errors.rejectValue("lossRateStr", "",
								"『损耗率』只能输入大于等于0小于100的数字");
					}
				} catch (Exception arg6) {
					errors.rejectValue("lossRateStr", "", "『损耗率』只能输入数字");
				}
			} else {
				errors.rejectValue("lossRateStr", "", "请输入『损耗率』");
				hasError = true;
			}
		}

		if (!hasError
				&& (form.getSequCode() == null || form.getSequCode().trim()
						.equals(""))) {
			errors.rejectValue("makeType.sequCode", "", "请输入『序号』");
			hasError = true;
		}

	}

	public MakeTypeDao getMakeTypeDao() {
		return this.makeTypeDao;
	}

	public void setMakeTypeDao(MakeTypeDao makeTypeDao) {
		this.makeTypeDao = makeTypeDao;
	}
}